home *** CD-ROM | disk | FTP | other *** search
- /*
- * Blob Manager Demonstration: Fish Puzzle module
- *
- * Cuts fish picture into pieces, allows user to reassemble.
- *
- * 26 July 1986 Paul DuBois
- *
- * 24 Dec 93
- * - Added Answer button so user can see answer if unable to solve puzzle.
- */
-
- # include "TransSkel.h"
-
-
- # include "BlobMgr.h"
- # include "BlobDemo.h"
-
-
- /*
- * The picture is 234 wide by 144 high
- */
-
- # define hOff 5 /* puzzle board offsets */
- # define vOff 5
- # define hWidth 18
- # define vHeight 144
- # define hGap 1
- # define vGap 1
- # define rows 1
- # define cols 13
- # define cells 13 /* i.e., rows x cols */
-
-
- static WindowPtr wind;
- static GrafPtr theFish;
- static BlobSetHandle receptors; /* receptor blobs */
- static BlobSetHandle misc;
- static BlobHandle scrambleBlob; /* simulated control */
- static BlobHandle answerBlob; /* simulated control */
- static Boolean paused;
-
- static PicHandle pic;
- static Rect picRect;
-
-
- /*
- * Make a receptor blob, glue it onto itself, and set it so that it
- * is matched when it is glued onto itself. This provides an easy
- * way of telling when the fish has been reassembled properly after
- * having been scrambled.
- */
-
- static void
- MakeRBlob (Rect *r)
- {
- BlobHandle b;
- static short pieceNo = 0;
- Rect r2;
-
- b = NewBlob (receptors, true, infiniteGlue, true, 0L);
- OpenBlob ();
- r2 = *r;
- OffsetRect (&r2, pieceNo * hWidth - r->left, -r->top);
- CopyBits (&theFish->portBits, &wind->portBits, &r2, r, srcCopy, nil);
- CloseRectBlob (b, r, r);
- NewBlobMatch (b, b);
- GlueGlob (b, b);
- ++pieceNo;
- }
-
-
- static void
- MakeBlobs (void)
- {
- receptors = NewBlobSet ();
- pic = (PicHandle) GetResource ('PICT', fshPictRes);
- picRect = (**pic).picFrame;
- OffsetRect (&picRect, -picRect.left, -picRect.top); /* to (0, 0) */
- theFish = NewOffPort (&picRect); /* draw fish into offscreen port */
- SetPort (theFish);
- DrawPicture(pic, &picRect);
- ReleaseResource ((Handle) pic);
- SetPort (wind);
- MakeBlobGrid (rows, cols, hOff, vOff, hWidth, vHeight,
- hGap, vGap, MakeRBlob);
- DisposeOffPort (theFish); /* throw away offscreen port */
- }
-
-
- static void
- Reset (void)
- {
- ShuffleGlobSet (receptors);
- HiliteBlob (scrambleBlob, inDragBlob, dimDraw);
- HiliteBlob (answerBlob, inDragBlob, normalDraw);
- paused = false;
- }
-
-
- static void
- Pause (void)
- {
- HiliteBlob (scrambleBlob, inDragBlob, normalDraw);
- HiliteBlob (answerBlob, inDragBlob, dimDraw);
- paused = true;
- }
-
-
- static pascal void
- Mouse (Point pt, long t, short mods)
- {
- BlobHandle b, d;
-
- if (TestBlob (scrambleBlob, pt) == inDragBlob)
- {
- if (BTrackMouse (scrambleBlob, pt, inFullBlob))
- Reset ();
- }
- else if (TestBlob (answerBlob, pt) == inDragBlob)
- {
- if (BTrackMouse (answerBlob, pt, inFullBlob))
- {
- /* reassemble to show answer */
- for (b = FirstBlob (receptors); b != (BlobHandle) nil; b = NextBlob (b))
- GlueGlob (b, b);
- Pause ();
- }
- }
- else if (!paused)
- {
- BlobClick (pt, t, nil, receptors);
- if (BlobSetQuiet (receptors))
- Pause ();
- }
- }
-
-
- static pascal void
- Update (Boolean resized)
- {
- Rect r;
-
- EraseRect (&wind->portRect);
- SetRect (&r, 0, 0,
- (hWidth + hGap) * cols + hGap + 4,
- vHeight + 2 * hGap + 4);
- OffsetRect (&r, hOff-hGap-2, vOff-vGap-2);
- PenMode (patBic);
- FrameRect (&r);
- PenNormal ();
- DrawGrid (rows, cols, hOff, vOff, hWidth, vHeight, hGap, vGap);
- DrawBlobSet (receptors);
- DrawBlob (scrambleBlob, inFullBlob);
- DrawBlob (answerBlob, inFullBlob);
- }
-
-
- static pascal void
- Activate (Boolean active)
- {
- if (active)
- {
- SetDragRects (wind);
- SetBCPermissions (false, false, false, true, true);
- }
- }
-
-
- void
- FshInit (void)
- {
- Rect r;
-
- SkelWindow (wind = GetDemoWind (fshWindRes),
- Mouse, /* mouse clicks */
- nil, /* key clicks */
- Update, /* updates */
- Activate, /* activate/deactivate events */
- nil, /* close window */
- DoWClobber, /* dispose of window */
- nil, /* idle proc */
- false); /* irrelevant, since no idle proc */
-
- BackPat (black);
- EraseRect (&wind->portRect);
- MakeBlobs ();
-
- misc = NewBlobSet ();
- SetRect (&r, 0, 0, 20, 134);
- OffsetRect (&r, wind->portRect.right - 46, 10);
- scrambleBlob = NewVButtonBlob (misc, &r, "\pScramble", true);
- HiliteBlob (scrambleBlob, inDragBlob, dimDraw);
- OffsetRect (&r, 23, 0);
- answerBlob = NewVButtonBlob (misc, &r, "\pAnswer", true);
- HiliteBlob (answerBlob, inDragBlob, normalDraw);
-
- MakeFrontWind (wind); /* show window */
- SkelPause (30L); /* wait a bit */
- Reset (); /* scramble fish */
- SkelDoUpdates (); /* process update resulting from scramble */
- }
-